package com.have.blog.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.domain.dto.ResourceCategoryDto;
import com.have.blog.admin.domain.dto.StudyVideoDto;
import com.have.blog.admin.service.VideoService;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.ResourceCategoryMapper;
import com.have.blog.model.mapper.VideoMapper;
import com.have.blog.model.models.ResourceCategory;
import com.have.blog.model.models.ResourceCategoryExample;
import com.have.blog.model.models.Video;
import com.have.blog.model.models.VideoExample;
import com.have.blog.picture.service.PictureFeignApi;
import com.have.blog.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private PictureFeignApi pictureFeignApi;
    @Autowired
    private ResourceCategoryMapper resourceCategoryMapper;

    @Override
    public List<StudyVideoDto> getPageList(StudyVideoDto studyVideoDto) {
        PageHelper.startPage(studyVideoDto.getPageNum(), studyVideoDto.getPageSize());
        VideoExample example = new VideoExample();
        VideoExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(studyVideoDto.getKeyword()) && !StringUtils.isEmpty(studyVideoDto.getKeyword().trim())) {
            criteria.andNameLike("%" + studyVideoDto.getKeyword() + "%");
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("create_time desc");
        List<Video> list = videoMapper.selectByExample(example);
        List<StudyVideoDto> resultList = MyBeanUtils.copyListProperties(list, StudyVideoDto::new);

        List<Integer> picUids = new ArrayList<>();
        List<Integer> categoryUids = new ArrayList<>();
        resultList.forEach(item -> {
            if (item.getCoverPictureUid() != null) {
                picUids.add(item.getCoverPictureUid());
            }
            if (item.getResourceCategoryUid() != null) {
                categoryUids.add(item.getResourceCategoryUid());
            }
        });
        if (!CollectionUtils.isEmpty(picUids)) {
            Wrapper<Map<Integer, String>> picWrapper = pictureFeignApi.getPicturesUrl(picUids);
            if (picWrapper.isSuccess()) {
                Map<Integer, String> picMap = picWrapper.getData();
                for (StudyVideoDto videoDto : resultList) {
                    videoDto.setPhotoUrl(picMap.get(videoDto.getCoverPictureUid()));
                }
            }
        }
        if (!CollectionUtils.isEmpty(categoryUids)) {
            ResourceCategoryExample categoryExample = new ResourceCategoryExample();
            categoryExample.createCriteria().andStatusEqualTo(BlogStatus.ENABLE.getStatus()).andUidIn(categoryUids);
            List<ResourceCategory> resourceCategories = resourceCategoryMapper.selectByExample(categoryExample);
            List<ResourceCategoryDto> resourceCategoryDtos = MyBeanUtils.copyListProperties(resourceCategories, ResourceCategoryDto::new);
            Map<Integer, ResourceCategoryDto> map = new HashMap<>();
            for (ResourceCategoryDto dto : resourceCategoryDtos) {
                map.put(dto.getUid(), dto);
            }
            for (StudyVideoDto dto : resultList) {
                dto.setResourceCategoryDto(map.get(dto.getResourceCategoryUid()));
            }
        }

        return resultList;
    }

    @Override
    public Wrapper addVideo(StudyVideoDto studyVideoDto) {
        Video studyVideo = new Video();
        MyBeanUtils.copyProperties(studyVideoDto, studyVideo);
        studyVideo.setClickCount(0);
        int result = videoMapper.insert(studyVideo);
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.INSERT_SUCCESS);
        }
        return Wrapper.successWithMessage(AdminMessageConstant.INSERT_FAIL);

    }

    @Override
    public Wrapper editVideo(StudyVideoDto studyVideoDto) {
        Video studyVideo = videoMapper.selectByPrimaryKey(studyVideoDto.getUid());
        studyVideo.setName(studyVideoDto.getName());
        studyVideo.setSummary(studyVideoDto.getSummary());
        studyVideo.setContent(studyVideoDto.getContent());
        studyVideo.setCoverPictureUid(studyVideoDto.getCoverPictureUid());
        studyVideo.setBaiduPath(studyVideoDto.getBaiduPath());
        studyVideo.setResourceCategoryUid(studyVideoDto.getResourceCategoryUid());
        int update = videoMapper.updateByPrimaryKeySelective(studyVideo);
        if (update > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
    }

    @Override
    public Wrapper deleteBatchVideo(List<Integer> uids) {
        if (CollectionUtils.isEmpty(uids)) {
            return Wrapper.illegal();
        }
        VideoExample example = new VideoExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andUidIn(uids);
        Video template = new Video();
        template.setStatus(BlogStatus.DISABLE.getStatus());
        int result = videoMapper.updateByExampleSelective(template, example);

        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.DELETE_SUCCESS);
        } else {
            return Wrapper.failed(AdminMessageConstant.DELETE_FAIL);
        }
    }
}
